home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 21 / Cream of the Crop 21 (Terry Blount) (October 1996).iso / program / libkb100.zip / LIBKB-1.00 / SAMPLES / TUBE.H < prev   
C/C++ Source or Header  |  1996-07-23  |  10KB  |  493 lines

  1. /* tube.h -- grpahics and sound wrapper
  2.  * Copyright (C) 1996 Markus F.X.J. Oberhumer
  3.  * For conditions of distribution and use, see copyright notice in kb.h 
  4.  */
  5.  
  6.  
  7. /***********************************************************************
  8. // Sound section
  9. ************************************************************************/
  10.  
  11. /* use the great MikMod soundsystem by Jean-Paul Mikkers */
  12. #if defined(USE_MIKMOD)
  13. #if defined(__DJGPP__) || defined(__WATCOMC__) || defined(__KB_LINUX)
  14. #  define HAVE_SOUND
  15. #  include <mikmod.h>
  16.    static UNIMOD *mod = NULL;
  17. #endif
  18. #if defined(__DJGPP__)
  19. #  include <crt0.h>
  20.    int _crt0_startup_flags = _CRT0_FLAG_NEARPTR | _CRT0_FLAG_LOCK_MEMORY;
  21. #endif
  22.  
  23. int MikMod_Setup(unsigned sample_rate)
  24. {
  25. /* Initialize soundcard parameters */
  26.     md_mixfreq    = sample_rate;               /* standard mixing freq */
  27.     md_dmabufsize = 16384;                     /* standard dma buf size */
  28.     md_mode       = DMODE_16BITS|DMODE_STEREO; /* standard mixing mode */
  29.     md_device     = 0;                         /* standard device: autodetect */
  30.  
  31. /* Register the loaders we want to use */
  32.     ML_RegisterLoader(&load_mod);
  33. #if 0
  34.     ML_RegisterLoader(&load_m15);/* if you use m15load, register it as first! */
  35.     ML_RegisterLoader(&load_mtm);
  36.     ML_RegisterLoader(&load_s3m);
  37.     ML_RegisterLoader(&load_stm);
  38.     ML_RegisterLoader(&load_ult);
  39.     ML_RegisterLoader(&load_xm);
  40.     ML_RegisterLoader(&load_uni);
  41. #endif
  42.  
  43. /* Register the drivers we want to use */
  44.     MD_RegisterDriver(&drv_nos);
  45. #ifdef __WIN32__
  46.     MD_RegisterDriver(&drv_w95);
  47. #else
  48.     MD_RegisterDriver(&drv_sb);
  49.     MD_RegisterDriver(&drv_gus);
  50. #endif
  51.  
  52.     return 0;
  53. }
  54.  
  55. int MikMod_PlayMod(UNIMOD *mod)
  56. {
  57.     MD_PlayStop();
  58.     if (mod == NULL)
  59.         return -1;
  60.     /* initialize modplayer to play this module */
  61.     MP_Init(mod);
  62.     /* set the number of voices to use */
  63.     if (md_numchn < mod->numchn)
  64.         md_numchn = mod->numchn;
  65.     /*  start playing the module: */
  66.     MD_PlayStart();
  67.     return 0;
  68. }
  69.  
  70. #endif
  71.  
  72.  
  73. /* use the Sound-Blaster Library v0.5 by Joel H. Hunter */
  74. #if defined(USE_SB_LIB)
  75. #if defined(__DJGPP__) && defined(__KB_MSDOS32)
  76. #  define HAVE_SOUND
  77. #  include <sb_lib.h>
  78.    static sb_mod_file *mod = NULL;
  79. #endif
  80. #endif
  81.  
  82.  
  83. /* use Varmint's Audio Tools v0.61 by Eric Jorgensen */
  84. #if defined(USE_VAT)
  85. #if defined(__WATCOMC__) && defined(__KB_MSDOS32)
  86. #  define HAVE_SOUND
  87. #  include <vat.h>                /* note: I renamed this header file */
  88.    static MOD *mod = NULL;
  89. #endif
  90. #endif
  91.  
  92.  
  93. /***********************************************************************
  94. // Linux svgalib wrapper
  95. ************************************************************************/
  96.  
  97. #if defined(__KB_LINUX)
  98.  
  99. #include <vga.h>
  100. #include <vgagl.h>
  101.  
  102. GraphicsContext physicalscreen;
  103.  
  104. int setmode13(int x)
  105. {
  106.     int modes[] = { G320x200x256, G320x240x256, G720x348x2, G640x480x256 };
  107.     int m;
  108.  
  109.     if (x < 0 || x > 3)
  110.         x = 0;
  111.     m = modes[x];
  112.  
  113.     vga_init();
  114.     if (vga_setmode(m) != 0)        /* <- this does the real init of svgalib */
  115.         return -1;
  116.     if (gl_setcontextvga(m) != 0)
  117.         return -1;
  118.     gl_getcontext(&physicalscreen);
  119.     /* gl_enableclipping(); */        /* not needed */
  120.     return 0;
  121. }
  122.  
  123. void setmode03(void)
  124. {
  125.     vga_setmode(TEXT);
  126. }
  127.  
  128. void setcolor(int c, int r, int g, int b)
  129. {
  130.     vga_setpalette(c,r,g,b);
  131. }
  132.  
  133. __inline__ void setpixel(int x, int y, int c)
  134. {
  135.     vga_setcolor(c);
  136.     vga_drawpixel(x,y);
  137. }
  138.  
  139. void waitvrt(void)
  140. {
  141.     vga_waitretrace();
  142. }
  143.  
  144. #endif
  145.     
  146.  
  147. /***********************************************************************
  148. // MSDOS graphics wrapper (Allegro)
  149. ************************************************************************/
  150.  
  151. #if defined(__KB_MSDOS) && defined(USE_ALLEGRO)
  152.  
  153. #include <allegro.h>
  154.  
  155. #define WIDTH    SCREEN_W
  156. #define HEIGHT    SCREEN_H
  157.  
  158. void setcolor(int c, int r, int g, int b)
  159. {
  160.     RGB rgb;
  161.  
  162.     rgb.r = r; rgb.g = g; rgb.b = b;
  163.     set_color(c, &rgb);
  164. }
  165.  
  166. int setmode13(int dummy)
  167. {
  168.     int c, w, h;
  169.     c = GFX_AUTODETECT; w = 320; h = 200;
  170.  
  171.     allegro_init();
  172.     if (is_win == 0)
  173.         install_mouse();
  174.     install_timer();
  175.     install_keyboard();
  176.     /* initialise_joystick(); */
  177.  
  178.     if (set_gfx_mode(GFX_VGA, 320, 200, 0, 0) != 0)
  179.     {
  180.         allegro_exit();
  181.         printf("Allegro: error setting graphics mode\n%s\n\n", allegro_error);
  182.         return -1;
  183.     }
  184.  
  185.     if (is_win == 0)
  186.     {
  187.         /* colors gfx_mode_select() */
  188.         setcolor(0, 0,0,0);
  189.         setcolor(1, 48,48,48);
  190.         setcolor(2, 56,10,10);
  191.         /* colors for mouse cursor */
  192.         setcolor(16, 0,0,0);
  193.         setcolor(255, 63,63,63);
  194.         clear(screen);
  195.         textout_centre(screen,font,"Allegro Setup",SCREEN_W/2,10,2);
  196.         gui_fg_color = 0;
  197.         gui_bg_color = 1;
  198.         if (!gfx_mode_select(&c, &w, &h))
  199.         {
  200.             allegro_exit();
  201.             return -1;
  202.         }
  203.         if (set_gfx_mode(c, w, h, 0, 0) != 0)
  204.         {
  205.             allegro_exit();
  206.             printf("Allegro: error setting graphics mode\n%s\n\n", allegro_error);
  207.             return -1;
  208.         }
  209.     }
  210.  
  211.     remove_keyboard();
  212.     clear(screen);
  213.     set_clip(screen,0,0,0,0);    /* clipping not needed */
  214.     return 0;
  215. }
  216.  
  217. void setmode03(void)
  218. {
  219.     allegro_exit();
  220. }
  221.  
  222. __inline__ void setpixel(int x, int y, int c)
  223. {
  224.     putpixel(screen,x,y,c);
  225. }
  226.  
  227. void waitvrt(void)
  228. {
  229.     vsync();
  230. }
  231.  
  232. #endif /* __KB_MSDOS */
  233.  
  234.  
  235. /***********************************************************************
  236. // MSDOS mode 13h graphics wrapper 
  237. ************************************************************************/
  238.  
  239. #if defined(__KB_MSDOS) && !defined(USE_ALLEGRO)
  240.  
  241. #if defined(__DJGPP__) && !defined(_CRT0_FLAG_LOCK_MEMORY)
  242. #  include <crt0.h>
  243.    int _crt0_startup_flags = _CRT0_FLAG_LOCK_MEMORY;
  244. #endif
  245.  
  246. #define WIDTH    320
  247. #define HEIGHT    200
  248.  
  249. #if defined(__KB_MSDOS16) && defined(__BORLANDC__)
  250. #  define VIDMEM    ((unsigned char far *) 0xa0000000l)
  251. #elif defined(__DJGPP__)
  252. #  include <sys/farptr.h>
  253. #  define VIDMEM    0xa0000
  254. #elif defined(__GO32__)
  255. #  define VIDMEM    ((unsigned char *) 0xd0000000)
  256. #elif defined(__EMX__)
  257.    static unsigned char *VIDMEM = NULL;
  258. #elif defined(__WATCOMC__)
  259. #  define VIDMEM    ((unsigned char *) 0xa0000)
  260. #endif
  261.  
  262.  
  263. #if defined(KB_INT86)
  264. int setmode13(int dummy)
  265. {
  266.     KB_INT86_REGS regs;
  267.     _kb_int86_regs_init_ax(®s,0x13);
  268.  
  269. #if defined(__EMX__)
  270.     if (!KB_USE_INT86())
  271.         return -1;            /* _int86() not allowed */
  272.     if (_portaccess(0x3c8, 0x3da) != 0)
  273.         return -1;
  274.     if (VIDMEM == NULL)
  275.         VIDMEM = _memaccess(0xa0000, 0xaffff, 1);
  276.     if (VIDMEM == NULL)
  277.         return -1;
  278. #endif
  279.     KB_INT86(0x10,®s);
  280.     return 0;
  281. }
  282.  
  283. void setmode03(void)
  284. {
  285.     KB_INT86_REGS regs;
  286.     _kb_int86_regs_init_ax(®s,0x03);
  287.  
  288. #if defined(__EMX__)
  289.     if (!KB_USE_INT86())
  290.         return;                /* _int86() not allowed */
  291. #endif
  292.     KB_INT86(0x10,®s);
  293. }
  294. #endif
  295.  
  296.  
  297. #if defined(__DJGPP__)
  298. __inline__ void setpixel(int x, int y, int c)
  299. {
  300.     _farnspokeb(VIDMEM + y*WIDTH + x, c);
  301. }
  302. #else
  303. __inline__ void setpixel(int x, int y, int c)
  304. {
  305.     *(VIDMEM + y*WIDTH + x) = c;
  306. }
  307. #endif
  308.  
  309.  
  310. void setcolor(int c, int r, int g, int b)
  311. {
  312.     /* fprintf(stderr,"%3d %3d %3d %3d\n",c,r,g,b); */
  313.     KB_OUTP8(0x3c8, c);    
  314.     _kb_usleep(5);
  315.     KB_OUTP8(0x3c9, r);
  316.     _kb_usleep(5);
  317.     KB_OUTP8(0x3c9, g);
  318.     _kb_usleep(5);
  319.     KB_OUTP8(0x3c9, b);
  320.     _kb_usleep(5);
  321. }
  322.  
  323. /* wait for the vertical retrace trailing edge */
  324. void waitvrt(void)
  325. {
  326.     while (!(KB_INP8(0x3da) & 0x8))        /* wait for retrace to end */
  327.         ;
  328.     while ((KB_INP8(0x3da) & 0x8))        /* wait for retrace to start again */
  329.         ;
  330. }
  331.  
  332. #endif /* __KB_MSDOS */
  333.  
  334.  
  335. /***********************************************************************
  336. // init sound library, load a MOD and play it in the background
  337. ************************************************************************/
  338.  
  339. #if defined(USE_MIKMOD)
  340.  
  341. static void tickhandler(void)
  342. {
  343.     MP_HandleTick();    /* play 1 tick of the module */
  344.     MD_SetBPM(mp_bpm);
  345. }
  346.  
  347. #endif
  348.  
  349.  
  350. int init_sound(const char *modfile, unsigned sample_rate)
  351. {
  352.     if (modfile == NULL || sample_rate < 5000)
  353.         return -1;
  354.  
  355. #if defined(USE_MIKMOD)
  356.     MikMod_Setup(sample_rate);
  357.     MD_RegisterPlayer(tickhandler);
  358.  
  359. /* initialize soundcard */
  360.     if (!MD_Init())
  361.     {
  362.         fprintf(stderr,"MikMod: Driver error: %s\n",myerr);
  363.         return -1;
  364.     }
  365.  
  366.     printf("MikMod info: Using %s for %d bit %s %s sound at %u Hz\n",
  367.             md_driver->Name,
  368.             (md_mode & DMODE_16BITS) ? 16 : 8,
  369.             (md_mode & DMODE_INTERP) ? "interpolated" : "normal",
  370.             (md_mode & DMODE_STEREO) ? "stereo" : "mono",
  371.             md_mixfreq);
  372.  
  373.     mod = ML_LoadFN((char *) modfile);
  374.     if (mod == NULL)
  375.     {
  376.         fprintf(stderr,"MikMod: MOD Error: %s\n",myerr);
  377.         return -1;
  378.     }
  379.     printf("Songname: %s\nModtype : %s\nPeriods : %s, %s\n",
  380.         mod->songname, mod->modtype,
  381.         (